var img_ = './fx.png';
var list = []

function getData() {
  let dListDom = document.getElementById('dList');
  let api = '/App/User/GetShipmentsInfo';
  let getPartnerFilePms = {};
  let data = {Vouchecode: 'FH-230404-00004', openid: 'En0014', Phone: '18200525426'};
  // let data = {Vouchecode: 'FH-230403-00004', openid: 'En0014', Phone: '18200525426'};
  // let data = {Vouchecode: 'FH-230404-00002', openid: 'En0014', Phone: '17608385812'};
  // let data = {Vouchecode: 'FH-230404-00001', openid: 'En0014', Phone: '13398153093'};
  data = getUrlCode();
  ajax('get', api, data, (res) => {
    let contentString = '';
    if (res.statusCode === 200) {
      list = res.data;
      let tNum = 0;
      for (let i = 0; i < list.length; i++) {
        let item = list[i];
        let sNum = parseInt(item.qty * item.num);
        tNum += sNum;
        contentString += `<div class="d-item">
        <div class="d-flex">
            <img class="item-img click-fun" cf="runDetail,${i}" src="${item.file ? item.file.path : false || img_}">
            <div>
                <div class="item-goodsName b-border click-fun" cf="runDetail,${i}">${item.goodsName || ''}</div>
                <div class="item-batchNo d-flex b-border">
                    <p class="item-title">生产号:</p>
                    <p class="item-content">${item.batchNo || ''}</p>
                </div>
                <div class="item-batchNo d-flex b-border">
                    <p class="item-title">规格:</p>
                    <p class="item-content">${item.specName || ''}</p>
                </div>
            </div>
        </div>
        <div class="d-flex b-border">
            <div class="d-flex w50">
                <p class="item-title">颜色:</p>
                <p class="item-content">${item.colourName || ''}</p>
            </div>
            <div class="d-flex w50">
                <p class="item-title">标准:</p>
                <p class="item-content">${item.standard || ''}</p>
            </div>
        </div>
        <div class="d-flex b-border">
            <div class="d-flex w50">
                <p class="item-title">尺寸:</p>
                <p class="item-content">${item.size || '0'}</p>
            </div>
            <div class="d-flex w50">
                <p class="item-title">方向:</p>
                <p class="item-content">${item.direction || ''}</p>
            </div>
        </div>
        <div class="d-flex b-border">
            <div class="d-flex w50">
                <p class="item-title">数量:</p>
                <p class="item-content">${item.qty + item.unit}</p>
            </div>
            <div class="d-flex w50">
                <p class="item-title">件数:</p>
                <p class="item-content">${sNum}</p>
            </div>
        </div>`;

        if (item.fileOutput && item.fileOutput.length > 0) {
          contentString += `
          <div class="d-flex b-border">
              <div class="d-flex w50">
              <div><p class="item-title">质检图片:</p></div><div>`;
          for (let item2 of item.fileOutput) {
            if (item2.path)
              contentString += `<img class="item-content-img click-fun" cf="imgClick" src="${item2.path}">`
          }
          contentString += ` </div>
              </div>
          </div>`;
        }

        contentString += `
         <div class="d-flex b-border">
            <div class="d-flex w100">
                <p class="item-title">备注:</p>
                <p class="item-content">${item.remark}</p>
            </div>
        </div>
    </div>`;


        // <div class="d-flex w50">
        //     <p class="item-title">运费:</p>
        //   <p class="item-content">￥${item.freightCharges}</p>
        //   </div>

      }

      contentString += `<div class="d-item total-item">总件数：${tNum}</div>`;

      getPartnerFilePms.openId = data.openid;
      getPartnerFilePms.idPartnerDTO = res.data[0].idPartnerDTO;
      getPartnerFilePms.idSendGoodsDTO = res.data[0].idSendGoodsDTO;

    }
    dListDom.innerHTML = contentString || '未查询到发货明细';
    getPartnerFile(getPartnerFilePms);
  }, (e) => {
    dListDom.innerHTML = '详情单异常！';
  });
}

function getPartnerFile(data) {
  let api = '/App/User/GetPartnerFile';
  let node = document.getElementById('dList');
  ajax('get', api, data, (res) => {
    if (res.data && res.data.length) {
      let contentString = node.innerHTML;
      contentString += `<div class="d-item"><div class="d-flex b-border">
              <div class="d-flex w50">
              <div><p class="item-title">票号:</p></div><div>`;
      for (let item2 of res.data) {
        if (item2.path)
          contentString += `<img class="item-content-img click-fun" cf="imgClick" src="${item2.path}">`
      }
      contentString += `</div></div>`;
      document.getElementById('dList').innerHTML = contentString
    }
    setClickFun();
  })
}


function setClickFun() {

  $('.click-fun').each((index) => {
    let item = $('.click-fun')[index];
    let params = item.getAttribute('cf') ? item.getAttribute('cf').split(',') : item.getAttribute('cf').split(',');
    $(item).on('click', (e) => {
      if (params[0] === 'imgClick') {
        setImgClick(e);
      } else if (params[0] === 'runDetail') {
        let map = list[parseInt(params[1])];
        redirect('/goods/detail.html', {
          id: map.idGoodsDTO,
          idPartnerDTO: map.idPartnerDTO,
          mobile: getUrlCode().Phone,
          openId: map.openId
        });
      }
    })
  })
}

function setImgClick(e) {
  let fun = (e) => {
    let imgs = [];
    for (let imgDom of e.target.parentElement.getElementsByTagName('img')) {
      imgs.push(imgDom.src);
    }
    // let path = $(this).attr('src');//这里的图片路径要直接能在浏览器中打开的完整的url路径
    let path = e.target.src;
    WeixinJSBridge.invoke('imagePreview',
      {
        'current': path,
        'urls': imgs
      }
    )
  };
  return fun(e);
  // $('img').on('click', fun);
}
